package com.jf3q.admin.controller;

import com.jf3q.admin.entity.User;
import com.jf3q.admin.service.impl.UserServiceImpl;
import com.jf3q.admin.utils.JwtUtil;
import com.jf3q.admin.utils.vo.AjaxResultVO;
import com.jf3q.admin.utils.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ASUS
 * @date 2024/4/16
 * @apiNote
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserServiceImpl userService;
    @Autowired
    PasswordEncoder passwordEncoder;

    @PostMapping("/login")
    public AjaxResultVO login(@RequestBody User loginUser) {
        User user;
        try {
            user = (User) userService.loadUserByUsername(loginUser.getUsername());
        } catch (UsernameNotFoundException e) {
            return AjaxResultVO.error("登录失败：" + e.getMessage());
        }
        if (!passwordEncoder.matches(loginUser.getPassword(), user.getPassword())) {
            return AjaxResultVO.error("登录失败：密码错误！");
        }
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword(), user.getAuthorities());
        SecurityContextHolder.getContext().setAuthentication(authenticationToken);
        String token = JwtUtil.getToken(user.getUsername());
        return AjaxResultVO.success(token);
    }

    @GetMapping("/logout")
    public AjaxResultVO logout() {
        return AjaxResultVO.success();
    }

    @GetMapping("/info")
    public AjaxResultVO info(String token) {
        String username = JwtUtil.getUsername(token);
        List<MenuVo> list = userService.getMyMenu(username);
        List<MenuVo> tree = new ArrayList<>();
        for (MenuVo menuVo : list) {
            menuVo.setHidden(menuVo.getType() == 3);
            if (menuVo.getPid().equals("1")) {
                for (MenuVo vo : list) {
                    if (vo.getPid().equals(menuVo.getId())) {
                        menuVo.getChildren().add(vo);
                    }
                }
                tree.add(menuVo);
            }
        }
        for (MenuVo menuVo : tree) {
            menuVo.setRedirect(menuVo.getPath() + "/" + menuVo.getChildren().get(0).getPath());
        }
        Map<String, Object> map=new HashMap<>();
        map.put("name",username);
        map.put("menus",tree);
        map.put("avatar","https://lxledu.oss-cn-hangzhou.aliyuncs.com/avatar/default.jpg");
        return AjaxResultVO.success(map);
    }

    @GetMapping("/xx")
    public AjaxResultVO xx() {
        return AjaxResultVO.success(userService.groupByTeacher());
    }

}
